aboutsummaryrefslogtreecommitdiffstats
path: root/src/routes/(app)/invoice/[id]/+layout.server.ts
blob: a0d7d676195c6923f0672cedd10695e9f5a780eb (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import { get } from "$lib/utils";
import { redirect } from "@sveltejs/kit";

export async function load({ cookies, depends, params, url, parent }) {
  depends("app:invoice");

  const token = cookies.get("token");
  let { subject, user } = await parent();
  const { id } = params;
  let invoice;

  if (id) {
    invoice = await get(`/invoice/${id}`);
    const options = !!url.searchParams.get("options");
    let { amount, pending, received } = invoice;
    amount = parseInt(amount);

    const paid =
      (!amount && (pending || received)) ||
      (amount > 0 && (pending >= amount || received >= amount));
    if (paid && !url.pathname.endsWith("paid")) {
      redirect(307, `/invoice/${id}/paid` + (options ? "?options=true" : ""));
    }

    subject = invoice.user;

    const aid = cookies.get("aid") || user?.id;
    if (
      user &&
      (invoice.uid !== user?.id || aid !== invoice.aid) &&
      !(url.pathname.includes("tip") || url.pathname.includes("memo")) &&
      !options
    ) {
      if (invoice.prompt && invoice.tip === null) redirect(307, `/invoice/${id}/tip`);
      else {
        redirect(307, `/send/invoice/${id}`);
      }
    }
  }

  const theme = cookies.get("theme") || "light";
  return { id, invoice, subject, user, token, theme };
}